package com.course.db;

import java.sql.ResultSet;

public class RealCourse extends ExecuteDB {

	//members
	private int Id;
	private int course_id;
	private int term_id;
	private int time_id;
	private String teach_location;
	private String strSql;
	
	public RealCourse()
	{
		this.Id = 0;
		this.course_id = 0;
		this.term_id = 0;
		this.time_id = 0;
		this.teach_location = "";
	}
	
	public String CountSql()
	{
		this.strSql = "select count(*) from real_course";
		return this.strSql;
	} 
	
	//add a RealCourse
	public boolean add()
	{

		this.strSql="insert into real_course ";
		this.strSql=this.strSql + "(course_id,term_id,time_id,teach_location) ";
		this.strSql=this.strSql + "values('" + this.course_id + "','" + this.term_id+ "','" + this.time_id + "','" + this.teach_location+ "')";
		System.out.println(this.strSql);
		boolean isAdd = super.exeSql(this.strSql);
        return isAdd;
	}
	
	public int lastid()
	{
		int lid = 0;
		this.strSql = "select last_insert_id() as id from real_course limit 1";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
			if (rs.next())
				lid = rs.getInt("id");
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return lid;
	}
	
	//delete a RealCourse by id
    public boolean delete(int RealCourseID)
    {
       this.strSql = "delete from real_course where Id in (";
       this.strSql = this.strSql + RealCourseID +")";

       boolean isDelete = super.exeSql(this.strSql);
       return isDelete;
    }

  //initialize RealCourse information
 	public boolean init()
	{
		this.strSql="select * from `real_course` where Id=";
		this.strSql=this.strSql + "'" + this.Id + "'";

		try
		{
			ResultSet rs = super.exeQuery(this.strSql);
			if (rs.next())
			{
				this.Id=rs.getInt("Id");
				this.course_id=rs.getInt("course_id");
				this.term_id=rs.getInt("term_id");
				this.time_id=rs.getInt("time_id");
				this.teach_location=rs.getString("teach_location");
				return true;
			}
			else
			{
				return false;
			}
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			return false;
		}
	}
 	
 	//modify RealCourse infomation
    public boolean modify_real_course()
	{
		this.strSql="update real_course set";
		this.strSql=this.strSql + " course_id=" + "'" + this.course_id + "',";
		this.strSql=this.strSql + " term_id=" + "'" + this.term_id + "',";
		this.strSql=this.strSql + " time_id=" + this.time_id + ",";
		this.strSql=this.strSql + " teach_location=" + this.teach_location;
		this.strSql=this.strSql + " where Id=" + this.Id;
		
		boolean isUpdate = super.exeSql(this.strSql);

		return isUpdate;
	}
    
  //show RealCourse
    public ResultSet show_real_course()
	{
		this.strSql="select * from `real_course`";
		ResultSet rs = null;
		try
		{
			rs = super.exeQuery(this.strSql);
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
		}
		return rs;
	}

	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public int getCourse_id() {
		return course_id;
	}

	public void setCourse_id(int course_id) {
		this.course_id = course_id;
	}

	public int getTerm_id() {
		return term_id;
	}

	public void setTerm_id(int term_id) {
		this.term_id = term_id;
	}

	public int getTime_id() {
		return time_id;
	}

	public void setTime_id(int time_id) {
		this.time_id = time_id;
	}

	public String getTeach_location() {
		return teach_location;
	}

	public void setTeach_location(String teach_location) {
		this.teach_location = teach_location;
	}

	public String getStrSql() {
		return strSql;
	}

	public void setStrSql(String strSql) {
		this.strSql = strSql;
	}

    
}
